vim.g.colors_name = "the unnamed (my colors)"

local function highlight(group, options)
    if type(group) == "string" then
        vim.api.nvim_set_hl(0, group, options)
    elseif type(group) == "table" then
        for _, value in ipairs(group) do
            vim.api.nvim_set_hl(0, value, options)
        end
    end
end

local function highlight_table(table)
    for k, v in pairs(table) do
        highlight(k, v)
    end
end

local my_colors = require("my_colors")
local transparent_black = my_colors.palette.black

if my_colors.config.transparent_background then
    transparent_black = "NONE"
end

function my_colors:set_syntax_highlight()
    highlight({
        "PreProc", "Include", "Define", "@keyword.import.c",
        "@keyword.directive.c", "@keyword.import.cpp", "@keyword.directive.cpp",
        "@keyword.directive.define.c", "@keyword.directive.define.cpp"
    }, {
        fg = self.palette.bright_magenta,
        bg = "NONE",
    })

    highlight({ "Constant", "Character", "Number", "Boolean", "Float" }, {
        fg = "#727272",
        bg = "NONE",
        bold = true,
        -- underline = true,
    })

    highlight({ "Title", "@markup.heading" }, {
        fg = self.palette.bright_green,
        bg = "NONE",
        bold = true,
    })

    highlight({ "Underlined", "@markup.underline" }, {
        fg = "NONE",
        bg = "NONE",
        underline = true,
    })

    highlight_table({
        String = { fg = self.palette.yellow, bold = true },

        Identifier = { fg = self.palette.grey },
        Function = { fg = self.palette.green },

        Statement = { fg = self.palette.bright_yellow },
        Conditional = { fg = self.palette.red, bold = true },
        Repeat = { fg = self.palette.red },
        Operator = { fg = self.palette.yellow },
        Keyword = { fg = self.palette.bright_yellow, bold = true },
        Exception = { fg = self.palette.red },

        Comment = { fg = self.palette.grey },
        Macro = { fg = self.palette.magenta },

        Type = { fg = self.palette.blue, italic = true },
        Structure = { fg = self.palette.blue, bold = true },
        Typedef = { fg = self.palette.blue, italic = true, bold = true },

        Special = { fg = self.palette.cyan, bold = true },
        ["@string.special"] = { fg = self.palette.cyan, bold = true },
        SpecialChar = { fg = self.palette.red },
        Delimiter = { fg = self.palette.bright_yellow, bold = true },

        ["@markup.strong"] = { bold = true },
        ["@markup.italic"] = { italic = true },
        ["@markup.strikethrough"] = { strikethrough = true },
        ["@markup.link"] = { fg = self.palette.cyan },
        ["@markup.link.url"] = { fg = self.palette.cyan, underline = true },
    })
end

-- TODO: leave here only setting the highlights, throw declarations to helper lib
my_colors:set_terminal_color()
my_colors:set_syntax_highlight()

highlight_table({
    -- NOTE: ui colors
    Normal = { fg = my_colors.palette.white, bg = transparent_black },
    NormalFloat = { fg = my_colors.palette.white, bg = transparent_black },
    ModeMsg = { fg = my_colors.palette.white_alt, bold = true },

    Terminal = { fg = my_colors.palette.white, bg = my_colors.palette.black },

    TabLine = { fg = my_colors.palette.white, bg = "#202020" },
    TabLineFill = { fg = my_colors.palette.white, bg = transparent_black },
    TabLineSel = { fg = my_colors.palette.white, bg = my_colors.palette.black },

    EndOfBuffer = { fg = my_colors.palette.black },

    ColorColumn = { bg = "#202020" },
    StatusLine = { fg = "#202020", bg = my_colors.palette.grey, reverse = true },

    CursorLine = { bg = "#1b1b1b" },
    CursorLineNr = { bg = my_colors.palette.black_alt },

    SignColumn = {
        fg = my_colors.palette.white,
        bg = my_colors.palette.black_alt,
    },
    LineNr = { fg = "#484848", bg = my_colors.palette.black_alt },

    Visual = { bg = "#282828" },
    Pmenu = { fg = my_colors.palette.white, bg = "#282828" },
    WinSeparator = { fg = "#323232" },

    Search = { bg = my_colors.palette.bright_black, bold = true },
    CurSearch = {
        fg = my_colors.palette.black,
        bg = my_colors.palette.grey,
        bold = true,
    },

    FloatTitle = {
        fg = my_colors.palette.grey,
        bg = my_colors.palette.black,
        bold = true,
    },

    ErrorMsg = { fg = my_colors.palette.black, bg = my_colors.palette.red },

    -- NOTE: indentblankline
    IblIndent = { fg = "#242424" },
    IblScope = { fg = "#484848" },
    IblWhitespace = { fg = my_colors.palette.black, bg = "NONE" },

    -- NOTE: gitsigns
    GitSignsAdd = {
        fg = my_colors.palette.bright_green,
        bg = my_colors.palette.black_alt,
        bold = true,
    },
    GitSignsAddLn = {
        fg = my_colors.palette.blue,
        bg = my_colors.palette.black_alt,
        bold = true,
    },
    GitSignsChange = {
        fg = my_colors.palette.grey,
        bg = my_colors.palette.black_alt,
        bold = true,
    },
    GitSignsDelete = {
        fg = my_colors.palette.red,
        bg = my_colors.palette.black_alt,
        bold = true,
    },
    GitSignsDeleteLn = {
        fg = my_colors.palette.red,
        bg = my_colors.palette.black_alt,
        bold = true,
    },

    -- NOTE: neotree
    NeoTreeFileIcon = {
        fg = my_colors.palette.yellow,
        bg = "NONE",
        bold = true,
    },
    NeoTreeDirectoryIcon = {
        fg = my_colors.palette.yellow,
        bg = "NONE",
        bold = true,
    },

    NeoTreeNormal = { fg = my_colors.palette.white },
    NeoTreeModified = { fg = my_colors.palette.yellow },
    NeoTreeTitleBar = {
        fg = my_colors.palette.white,
        bg = "#242424",
        bold = true,
    },
    NeoTreeFloatBorder = { fg = "#242424", bg = my_colors.palette.black },

    NeoTreeIndentMarker = { fg = my_colors.palette.grey },

    NeoTreeDirectoryName = { fg = my_colors.palette.green, bold = true },

    NeoTreeGitAdded = { fg = my_colors.palette.bright_green, bold = true },
    NeoTreeGitStaged = { fg = my_colors.palette.blue, bold = true },
    NeoTreeGitDeleted = { fg = my_colors.palette.red, bold = true },
    NeoTreeGitIgnored = { fg = "#484848", bold = true },
    NeoTreeGitRenamed = { fg = my_colors.palette.yellow, bold = true },
    NeoTreeGitConflict = { fg = my_colors.palette.bright_red, bold = true },
    NeoTreeGitModified = { fg = my_colors.palette.magenta, bold = true },
    NeoTreeGitUnstaged = { fg = my_colors.palette.grey, bold = true },
    NeoTreeGitUntracked = { fg = my_colors.palette.grey, bold = true },

    -- NOTE: experiments
    TodoSignWARN = { fg = my_colors.palette.black },
    DiagnosticSignHint = {
        fg = my_colors.palette.white,
        bg = my_colors.palette.black_alt,
    },
    DiagnosticSignInfo = {
        fg = my_colors.palette.blue,
        bg = my_colors.palette.black_alt,
    },
    DiagnosticSignWarn = {
        fg = my_colors.palette.yellow,
        bg = my_colors.palette.black_alt,
    },
    DiagnosticSignError = {
        fg = my_colors.palette.red,
        bg = my_colors.palette.black_alt,
    },

    LazyNormal = { fg = my_colors.palette.white, bg = "#282828" },
    LazyProgressTodo = { fg = my_colors.palette.white, bg = "#282828" },

    MasonNormal = { fg = my_colors.palette.white, bg = "#282828" },

    NeogitDiffAddHighlight = {
        fg = my_colors.palette.white,
        bg = my_colors.palette.green,
    },
    NeogitDiffAdd = {
        fg = my_colors.palette.grey,
        bg = my_colors.palette.green,
    },

    NeogitDiffDelete = {
        fg = my_colors.palette.grey,
        bg = my_colors.palette.red,
    },
    NeogitDiffDeleteHighlight = {
        fg = my_colors.palette.black,
        bg = my_colors.palette.red,
    },
})
